package com.api.controller;

import com.api.paramsVO.SendSmsParams;
import com.common.annotation.PassToken;
import com.common.apiResult.ApiController;
import com.common.apiResult.ApiResult;
import net.guerlab.sms.server.service.VerificationCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.naming.Binding;
import java.util.Objects;

/**
 * @ClassName SmsApiController
 * @Description TODO
 * @Date 2020/10/27 15:51
 * Create By Renbowen
 */
@RestController
@RequestMapping(value = "/api/sms")
public class SmsApiController extends ApiController {

    @Autowired
    private
    VerificationCodeService codeService;

    @RequestMapping(value = "/sendSms",method = RequestMethod.POST)
    @PassToken
    public ApiResult sendSms(@Validated SendSmsParams params, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ApiResult.fail(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }
        codeService.send(params.getPhone());
        return ApiResult.ok();
    }

}
